﻿using System;
using System.Collections.Generic;
using System.Text;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Model
{
    /// <summary>
    /// 获取当前对象的数据buffer选项
    /// </summary>
    public class TransferDataOptions
    {
        private static TransferDataOptions _default = null;
        /// <summary>
        /// 获取或设置选项默认值
        /// </summary>
        public static TransferDataOptions Default
        {
            get { return _default; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value), "选项默认值不能为null");
                }

                _default = value;
            }
        }

        static TransferDataOptions()
        {
            _default = new TransferDataOptions();
        }


        private int _limit = ArrayEx.MaxLength;
        /// <summary>
        /// 获取或设置数据长度门限,当数据长度小于此值时,返回数据类型为BufferDataType.Bytes
        /// </summary>
        public int Limit
        {
            get { return _limit; }
            set
            {
                const int MIN_LIMIT = 1;
                if (value < MIN_LIMIT || value > ArrayEx.MaxLength)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"门限值范围{MIN_LIMIT}-{ArrayEx.MaxLength}");
                }

                _limit = value;
            }
        }

        /// <summary>
        /// 获取或设置当数据长度大于门限值时,返回数据类型
        /// </summary>
        public TransferDataOptionsLimitDataType LimitType { get; set; } = TransferDataOptionsLimitDataType.MemoryMappedFile;


        private string _bufferDataFileDirectory = null;
        /// <summary>
        /// 获取或设置BufferData数据文件存放目录
        /// </summary>
        public string BufferDataFileDirectory
        {
            get { return _bufferDataFileDirectory; }
            set { _bufferDataFileDirectory = value; }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public TransferDataOptions()
        {

        }


        /// <summary>
        /// 生成BufferData数据文件路径
        /// </summary>
        /// <returns></returns>
        public string GenerateBufferDataFilePath()
        {
            string dir = this._bufferDataFileDirectory;
            string fileName = Guid.NewGuid().ToString();

            if (string.IsNullOrWhiteSpace(dir))
            {
                return fileName;
            }
            else
            {
                DirectoryInfoEx.CheckDirectory(dir);
                return PathEx.Combine(PathType.Local, dir, fileName);
            }
        }

    }

    /// <summary>
    /// 当数据长度大于门限值时,返回数据类型
    /// </summary>
    public enum TransferDataOptionsLimitDataType
    {
        /// <summary>
        /// 文件
        /// </summary>
        File,

        /// <summary>
        /// 内存映射文件
        /// </summary>
        MemoryMappedFile
    }


}
